const pageConf = require('../configs/config').page_conf;
const xiaomiData = require('../models/xiaomiData');
const extention = require('../lib/extention');

class IndexController {
    async index(ctx, next) {
        const { 
            swiper_data: swiperData, 
            field_data: fieldData, 
            phone_data: phoneData 
        } = await xiaomiData.getXiaomiDatas({ 
            phone: true, 
            swiper: true, 
            field: true 
        });
        await ctx.render('index', {
            pageConf: pageConf.index,
            swiperData,
            phoneData,
            fieldData
        })
    }
    async list(ctx, next) {
        const keyword = ctx.params.keyword;
        const {
            field_data: fieldData, 
            phone_data: phoneData 
        } = await xiaomiData.getXiaomiDatas({ 
            phone: true, 
            swiper: false, 
            field: true 
        });
        const searchData = extention.getSearchData(phoneData, keyword);
        await ctx.render('list', {
            pageConf: pageConf.list,
            phoneData,
            searchData,
            fieldData
        })
    }
    async detail(ctx, next) {
        const phoneId = ctx.params.id;
        const {
            field_data: fieldData, 
            phone_data: phoneData 
        } = await xiaomiData.getXiaomiDatas({ 
            phone: true, 
            swiper: false, 
            field: true 
        });
        await ctx.render('detail', {
            pageConf: pageConf.detail,
            phoneData,
            phoneDetail: phoneData.filter(item => item.id == phoneId)[0],
            fieldData
        })
    }
    async my404(ctx, next) {
        const {
            field_data: fieldData, 
            phone_data: phoneData 
        } = await xiaomiData.getXiaomiDatas({ 
            phone: true, 
            swiper: false, 
            field: true 
        });
        await ctx.render('404', {
            pageConf: pageConf[404],
            phoneData,
            fieldData
        })
    }
}

module.exports = new IndexController();